//
//  ECForgetPasswordViewController.m
//  Edu_iOS
//
//  Created by apple on 2017/9/11.
//  Copyright © 2017年 yanyan. All rights reserved.
//
#import "ECForgetPasswordViewController.h"
#import "ECNewPasswordViewController.h"

#import "ECUITextField_Account.h"
#import "CYDataHelper.h"
#import "UINavigationBar+Awesome.h"
#import "UIImage+Color.h"
#import "ECRegularExpression.h"

@interface ECForgetPasswordViewController (){
    ///发送验证码之后返回的token,用于登录/注册
    NSString *tokenOfCode;
}
@property (nonatomic, strong) ECUITextField_Account *phoneArea;
@property (nonatomic, strong) ECUITextField_Account *codeArea;
@end

@implementation ECForgetPasswordViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar lt_reset];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigation_Return"] style:UIBarButtonItemStylePlain target:self action:@selector(action_dismiss)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],
    NSForegroundColorAttributeName:[UIColor colorWithRed:0.13f green:0.13f blue:0.13f alpha:1.00f]}];
    
    /*  导航栏线  */
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#BFBFBF"] size:CGSizeMake(self.view.frame.size.width, 0.5)]];
    }

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"忘记密码";
    
    self.phoneArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60), kScreenWidth - kProportion_Width(60*2), kProportion_Height(Height_textfield)) placeholder:@"手机号" leftImage:[UIImage imageNamed:@"tel_user"]];
    [self.view addSubview:self.phoneArea];
    self.phoneArea.textField.keyboardType = UIKeyboardTypePhonePad;
    
    self.codeArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), CGRectGetMaxY(self.phoneArea.frame) + kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2), kProportion_Height(Height_textfield)) placeholder:@"验证码" leftImage:[UIImage imageNamed:@"registered_code"]];
    self.codeArea.textField.frame = CGRectMake(self.codeArea.textField.mj_x, self.codeArea.textField.mj_y, kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) - kProportion_Width(268) - 5, self.codeArea.textField.mj_h);
    [self.codeArea.rightButton setTitle:@"获取验证码" forState:0];
    self.codeArea.rightButton.titleLabel.font = [UIFont systemFontOfSize:14*kPercentage];
    [self.codeArea.rightButton setTitleColor: [UIColor colorWithHexString:@"#313131"] forState:0];
    [self.codeArea.rightButton setAdjustsImageWhenHighlighted:NO];
    self.codeArea.rightButton.frame = CGRectMake(CGRectGetMaxX(self.codeArea.textField.frame), 0, kProportion_Width(268), self.codeArea.mj_h);
    self.codeArea.rightButton.titleLabel.font = [UIFont systemFontOfSize:15*kPercentage];
    [self.view addSubview:self.codeArea];
    WeakSelf(weakSelf);
    self.codeArea.block_rightButton = ^(id obj) {
        //获取验证码处理
        
        //获取验证码处理
        if ([ECRegularExpression isPhoneNumber:weakSelf.phoneArea.textField.text] == YES){
            [HWHttpTool post:URL_POST_SmsCodes
                      params:@{@"type":weakSelf.type_sendCode,
                               @"mobile": weakSelf.phoneArea.textField.text}
                     success:^(id json) {
                         if ([json[@"code"] integerValue] == 200) {
                             [MBProgressHUD showTipMessageInWindow:@"获取验证码成功"];
                             tokenOfCode = json[@"data"][@"token"];
                             [weakSelf.codeArea.rightButton openCountdown];//判断成功之后进行倒计时
                         }else{
                             [MBProgressHUD showTipMessageInWindow:json[@"message"]];//返回错误提示
                         }
                     } failure:^(NSError *error) {
                         [MBProgressHUD showTipMessageInWindow:@"获取验证码失败"];
                     }];
        }else{
            [MBProgressHUD showTipMessageInWindow:@"请输入合法的手机号"];
        }
        
    };
    
    UIView *lineView = [[UIView alloc]init];
    [self.codeArea addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#B3B3B3"];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.codeArea.mas_centerY);
        make.right.mas_equalTo(self.codeArea.rightButton.mas_left).mas_offset(-3);
        make.height.offset(kProportion_Width(45));
        make.width.offset(1);
    }];
    
    ECUButton *registeredBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    registeredBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(self.codeArea.frame) + kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
    [self.view addSubview:registeredBtn];
    [registeredBtn setTitle:@"下一步" forState:0];
    [registeredBtn setTitleColor:[UIColor colorWithRed:255.f/255.f green:103.f/255.f blue:0 alpha:1.00f] forState:0];
    [registeredBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    registeredBtn.titleLabel.font = [UIFont systemFontOfSize:16*kPercentage];
    [registeredBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registeredBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)nextBtnClick{
    
    if ([CYDataHelper isNull:self.phoneArea.textField.text] == NO) {
        if ([CYDataHelper isNull:self.codeArea.textField.text] == NO) {
            DLog(@"下一步");
            //处理提交逻辑
            [self requestData];
            
            
        }else{
            [MBProgressHUD showTipMessageInWindow:@"验证码不能为空"];
        }
    }else{
        [MBProgressHUD showTipMessageInWindow:@"请输入正确的手机号"];
        
    }
}

//验证手机短信验证码
-(void)requestData{
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_CheckSmsCode
                           params:@{
                                    @"verifiedMobile":self.phoneArea.textField.text,
                                    @"smsCode":self.codeArea.textField.text,
                                    @"type":self.type_checkNext,
                                    @"token":tokenOfCode,
                                    }
                          headers:nil
                          success:^(id json) {
                              if ([json[@"code"] integerValue] == 200) {
                                  ECNewPasswordViewController *vc = [[ECNewPasswordViewController alloc]init];
                                  vc.telPhone = self.phoneArea.textField.text;
                                  [self.navigationController pushViewController:vc animated:YES];
                              }else{
                                  [MBProgressHUD showTipMessageInWindow:json[@"message"]];//返回错误提示
                              }
                          }
                          failure:^(NSError *error) {
                              [MBProgressHUD showTipMessageInWindow:@"网络异常,请稍后再试"];//返回错误提示
                          }];
}

-(void)action_dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
